package com.foosun.core.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Nelin on 14-3-9.
 */
public class PolicyServer implements Runnable {

    private final int policy_port = 843;
    private boolean status = true;

    private ServerSocket server = null;

    @Override
    public void run() {
        //创建安全验证服务器
        try {
            server = new ServerSocket(policy_port);
            while (status) {
                Socket socket = server.accept();
                new Thread(new PolicyThread(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }




    /**
     * 启动服务器
     */
    public void startPolicy() {
        new Thread(this).start();
    }

    /**
     * 关闭服务器
     */
    public void stopPolicy() {
        status = false;
        if (server != null && !server.isClosed()) {
            try {
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class PolicyThread implements Runnable {
    private final String policy_xml = "<policy-file-request/>";
    private final String cross_xml = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";
    private Socket socket;

    public PolicyThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //接收并发送Flex安全验证请求
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            char[] by = new char[22];
            br.read(by, 0, 22);
            String s = new String(by);
            if (s.equals(policy_xml)) {
                System.out.println("接收policy-file-request认证");
                pw.print(cross_xml);
                pw.flush();
                br.close();
                pw.close();
                socket.close();
                System.out.println("完成policy-file-request认证");
            }
        } catch (IOException e) {
            if (!socket.isClosed()) {
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            throw new RuntimeException("执行policy认证时发生异常", e);
        }
    }

}